package com.jci.xyf.hellofx;

//import com.mysql.jdbc.Driver;

import java.sql.*;

public class mysqlconn{
    Connection conn=null;
    Statement stmt=null;
    public void startmysql() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //连接数据库
            String url = "jdbc:mysql://localhost:3306/javafx?serverTimezone=Asia/Shanghai";
            String user = "root";
            String password = "123456";
            conn = DriverManager.getConnection(url, user, password);
            //获取数据库操作对象
            stmt = conn.createStatement();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void updata(String sql){
        //处理SQL语句
        try {
            stmt.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public ResultSet query(String sql){
        ResultSet result=null;
        try {
            //处理SQL语句
            result = stmt.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }
    public void close() {
        //释放资源
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
